<?php

namespace App\Admin\Controllers;

use \App\Models\Singer;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\AdminController;

class SingerController extends Controller
{
    use AdminController;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('歌手管理');
            $content->description('');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('歌手管理');
            $content->description('');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('歌手管理');
            $content->description('');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Singer::class, function (Grid $grid) {

            $grid->id('ID')->sortable();

            $grid->column('nick', '昵称');
            $grid->column('area', '地区');


            $grid->column('final', '决赛名单')->value(function ($val) {

                if ($val == 1) {
                    return '<span class="badge bg-green">决赛12强</span>';
                } else {
                    return '<span class="badge bg">非决赛</span>';
                }
            });


            $grid->created_at();
            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Singer::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->text('nick', '昵称')->rules('required');
            $form->text('area', '地区')->rules('required');
            $form->email('email')->rules('email');


            $form->text('video', '优酷视频通用代码');

            $form->image('avatar'); //->crop(300, 300);

//            $form->switch('final', '决赛12强')->states(['on' => 1, 'off' => 0]);

            $form->radio('final', '决赛12强')->values(['1' => '是', '0'=> '否']);

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
